package com.example.filesynsystem;

import com.example.filesynsystem.pojo.FileData;
import com.example.filesynsystem.util.AwsFileUtil;
import com.example.filesynsystem.util.LocFileUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

@Component
@EnableScheduling
@EnableAsync
public class Run {
    @Resource
    private AwsFileUtil awsFileUtil;

    @Resource
    private LocFileUtil locFileUtil;

    //本地文件
    @Value("${s3.localPath}")
    private String localPath;

    private Long limitSize = 15*1024*1024L;

    //用来执行的时候上锁
    private Integer startLocked = 0;

    //中间流程上锁
    private Integer processLocked = 0;

    //用来判断是否已经更新
    private Integer updateOrNot = 0;

    @Async
    @Scheduled(cron = "0/5 * * * * ?")
    public void checkTimely() {
        if (startLocked == 0) {
            startSyn();
        }
        if (processLocked == 0) {
            synToServer();
        }
    }


    //从服务器同步
    public void startSyn() {
        startLocked = 1;
        List<FileData> s3FileDatas = awsFileUtil.getFileData();
        List<FileData> localFileDatas = locFileUtil.getFileData();

        for (int i=0; i < s3FileDatas.size();i++) {
            String s3FileName = s3FileDatas.get(i).getKeyName();
            Date s3FileDate = s3FileDatas.get(i).getModifyTime();
            Long s3FileSize = s3FileDatas.get(i).getSize();
            updateOrNot = 0;

            for (int j=0; j<localFileDatas.size(); j++) {
                String localFileName = localFileDatas.get(j).getKeyName();
                Date localFileDate = localFileDatas.get(j).getModifyTime();
                Long localFileSize = localFileDatas.get(j).getSize();

                //若名字相同，则查看时间是否相同，若相同则再判断日期是否相同，若不同则覆盖，否则继续
                if (s3FileName.equals(localFileName)) {
                    if(s3FileDate.compareTo(localFileDate)==0) {
                        updateOrNot = 1;
                        break;
                    }
                    if (s3FileSize >= limitSize) {
                        awsFileUtil.downloadBigFile(s3FileName);
                        File file = new File(localPath + "\\"+ s3FileName);
                        file.setLastModified(s3FileDate.getTime());
                        updateOrNot = 1;
                        break;
                    }
                }
            }
            if (updateOrNot == 1) {
                continue;
            }
            if (s3FileSize >= limitSize) {
                awsFileUtil.downloadBigFile(s3FileName);
                File file = new File(localPath + "\\"+ s3FileName);
                file.setLastModified(s3FileDate.getTime());
                continue;
            }
            awsFileUtil.downloadFile(s3FileName);
            File file = new File(localPath + "\\"+ s3FileName);
            file.setLastModified(s3FileDate.getTime());
        }
    }

    //本地修改后，同步到服务器
    public void synToServer() {
        processLocked = 1;
        List<FileData> s3FileDatas = awsFileUtil.getFileData();
        List<FileData> localFileDatas = locFileUtil.getFileData();

        for (int i=0; i < localFileDatas.size();i++) {
            String localFileName = localFileDatas.get(i).getKeyName();
            Date localFileDate = localFileDatas.get(i).getModifyTime();
            Long localFileSize = localFileDatas.get(i).getSize();
            updateOrNot = 0;

            for (int j=0; j<s3FileDatas.size(); j++) {
                String s3FileName = s3FileDatas.get(j).getKeyName();
                Date s3FileDate = s3FileDatas.get(j).getModifyTime();
                Long s3FileSize = s3FileDatas.get(j).getSize();

                //若名字相同，则查看时间是否相同，若相同则再判断日期是否相同，若不同则覆盖，否则继续
                if (s3FileName.equals(localFileName)) {
                    if(s3FileDate.compareTo(localFileDate)==0) {
                        updateOrNot = 1;
                        break;
                    }
                    if (localFileSize >= limitSize) {
                        awsFileUtil.uploadBigFile(localFileName);
                        File file = new File(localPath + "\\"+ s3FileName);
                        file.setLastModified(s3FileDate.getTime());
                        updateOrNot = 1;
                        break;
                    } else {
                        awsFileUtil.uploadFile(localFileName);
                        File file = new File(localPath + "\\"+ s3FileName);
                        file.setLastModified(s3FileDate.getTime());
                        updateOrNot = 1;
                        break;
                    }
                }
            }
            if (updateOrNot == 1) {
                continue;
            }
            if (localFileSize >= limitSize) {
                awsFileUtil.uploadBigFile(localFileName);
                continue;
            } else {
                awsFileUtil.uploadFile(localFileName);
            }

        }

        //判断是否删除
        for (int i=0; i < s3FileDatas.size(); i++) {
            int deleteOrNot = 1;
            String s3FileName = s3FileDatas.get(i).getKeyName();
            for (int j=0; j<localFileDatas.size(); j++) {
                String localFileName = localFileDatas.get(i).getKeyName();
                if (s3FileName.equals(localFileName)) {
                    deleteOrNot = 0;
                    break;
                }
            }
            if (deleteOrNot == 1) {
                awsFileUtil.deleteFile(s3FileName);
            }
        }
        processLocked = 0;
    }
}
